package com.easylive.entity.config;

import org.apache.logging.log4j.core.config.AbstractConfiguration;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

@Configuration
public class EsConfiguration extends AbstractElasticsearchConfiguration implements DisposableBean {

    @Autowired
    private AppConfig appConfig;

    private RestHighLevelClient client;

    @Override
    public void destroy() throws Exception {
        if(client != null) {
            client.close();
        }
    }

    @Override
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration=ClientConfiguration.builder().connectedTo(appConfig.getEsHostPort()).build();
        client=RestClients.create(clientConfiguration).rest();
        return client;
    }
}
